<template>
  <div class="exam">
    <div class="main">
      <div class="header-wrapper">
        <div class="inner">
          <el-row>
            <el-col :span="18">
              <div class="grid-content bg-purple ovf left">
                <div class="logo">
                  <router-link to="/">
                    <img src="../../assets/images/logo.png" />
                  </router-link>
                </div>
                <div class="index"></div>
              </div>
            </el-col>
          </el-row>
        </div>
      </div>
      <div class="mian-body">
        <div class="main-center">
          <div class="body-wrapper">
            <div class="questions">
              <div class="questions-title">{{ title }}</div>
              <div class="questions-content">
                <div
                  class="question-content"
                  v-for="(item, i) in questionList"
                  :key="i"
                >
                  <div class="exam-question">
                    <span class="question-index ellipsis">{{ i + 1 }}.</span>
                    {{
                      (item.type == 1
                        ? "（单选题）"
                        : item.type == 2
                        ? "（多选题）"
                        : "（文本题）") + item.title
                    }}
                  </div>

                  <div v-if="item.type == 1 || item.type == 2">
                    <div
                      class="roseChart"
                      style="width: 100%; height: 300px"
                    ></div>
                  </div>
                  <div v-if="item.type == 3">
                    <div>
                      <el-table :data="textAns[i]" stripe style="width: 100%">
                        <el-table-column prop="index" label="用户" width="180">
                        </el-table-column>
                        <el-table-column prop="data" label="回答">
                        </el-table-column>
                      </el-table>
                    </div>
                    <div
                      class="roseChart"
                      style="width: 100px; height: 200px"
                    ></div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import * as echarts from "echarts";

export default {
  data() {
    return {
      title: "",
      name: "",
      qid: "",

      questionList: [],
      ansList: [],
      textAns: [],
    };
  },

  methods: {
    initChart() {
      console.log("initChart:");
      //   var echarts = require("echarts");
      var roseCharts = document.getElementsByClassName("roseChart"); // 对应地使用ByClassName

      for (let index in roseCharts) {
        // 通过for循环，在相同class的dom内绘制元素
        var myChart = echarts.init(roseCharts[index]);
        var question = this.questionList[index];
        var options = question.options;

        //单选
        if (question.type == 1) {
          var arr = []; //每个选项的文本
          var dataNum = []; //每个选项的数量
          for (var i = 0; i < options.length; i++) {
            arr.push(options[i].text);
            dataNum.push(0);
          }
          for (var i = 0; i < this.ansList.length; i++) {
            var answer = JSON.parse(this.ansList[i].answer);
            dataNum[answer[index]] = dataNum[answer[index]] + 1;
          }
          var option = {
            tooltip: {
              trigger: "axis",
            },
            xAxis: {
              type: "value",
            },
            yAxis: {
              x: "center",
              type: "category",
              data: arr,
              show: true,
            },
            series: [
              {
                name: "人数",
                type: "bar",
                smooth: true,
                data: dataNum,
              },
            ],
          };
          //   console.log("option:");
          //   console.log(option);
          myChart.setOption(option);
        } else if (question.type == 2) {
          var arr = [];
          var dataNum = [];
          for (let i in options) {
            arr.push(options[i].text);
            dataNum.push(0);
          }
          for (let i in this.ansList) {
            var answer = JSON.parse(this.ansList[i].answer);
            for (let j in answer[index]) {
              dataNum[answer[index][j]] = dataNum[answer[index][j]] + 1;
            }
            console.log(answer[index]);
            console.log(dataNum);
          }
          var option = {
            tooltip: {
              trigger: "axis",
            },
            xAxis: {
              type: "value",
            },
            yAxis: {
              x: "center",
              type: "category",
              data: arr,
              show: true,
            },
            series: [
              {
                name: "人数",
                type: "bar",
                smooth: true,
                data: dataNum,
              },
            ],
          };
          // console.log("option:");
          // console.log(option);
          myChart.setOption(option);
        } else if (question.type == 3) {
          this.$set(this.textAns, index, []);
          // console.log(this.ansList)
          for (let i in this.ansList) {
            var answer = JSON.parse(this.ansList[i].answer);
            this.textAns[index].push({ index: this.ansList[i].name, data: answer[index] });
          }
          // console.log(this.textAns[index]);
        }
      }
    },

    //获取答卷数据
    async getData() {
      const { data: res } = await this.$http.post("/questionnaireAnswered", {
        qId: this.qid,
        name: "",
      });

      if (res.code == "666") {
        this.ansList = res.data;
        this.initChart();
      }
    },

    //获取问卷样式
    async getQuestionList() {
      this.name = this.$route.query.name;
      this.qid = this.$route.query.qid;
      console.log(this.qid);

      const { data: res } = await this.$http.post(
        "/queryQuestionnaireByQidN?qId=" + this.qid
      );

      if (res.code == "666") {
        var question = res.data[0];
        this.questionList = JSON.parse(question.content);
        this.length = this.questionList.length;
        this.title = question.title;
        // console.log("questionList: ");
        // console.log(this.questionList);
      }

      this.getData();
    },
  },
  mounted: function () {
    this.getQuestionList();
  },
  //   onLoad(options) {
  //     if (options.qId) {
  //       this.qId = options.qId;
  //     }
  //   },
  beforeRouteEnter(to, from, next) {
    next(() => {
      // 改变html背景
      document.querySelector("html").style.cssText = `
        background: #ecf1f6;
      `;
    });
  },
};
</script>

<style scoped lang="scss">
/deep/ .el-progress__text {
  display: none;
}
/deep/ .el-select .el-input__inner:focus {
  border-color: #55b6da;
}
/deep/ .el-select {
  border: none;
}
/deep/ .el-select-dropdown__item.selected {
  color: #55b6da !important;
  font-weight: bold;
}
/deep/ .el-radio__input.is-checked + .el-radio__label {
  color: #55b6da;
}
/deep/ .el-radio__input.is-checked .el-radio__inner {
  border-color: #55b6da;
  background: #55b6da;
}
/deep/ .el-radio__inner:hover {
  border-color: #55b6da;
}
/deep/ .el-radio {
  display: block;
  margin: 20px 0;
}
.inner {
  padding: 0px 90px;
}
.exam {
  background: #ecf1f6;
  height: 100%;
  min-height: 100%;
}
.main {
  .header-wrapper {
    height: 80px;
    width: 100%;
    line-height: 80px;
    background: #ffffff;
    box-shadow: 0 2px 4px 0 rgba(153, 153, 153, 0.1);
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1040;
    .left {
      display: flex;
      align-items: center;
      font-size: 18px;
      .logo {
        width: 150px;
        margin-right: 120px;
        img {
          width: 100%;
        }
      }
    }
    .right {
      div {
        float: right;
        font-size: 16px;
      }
      .esc {
        width: 120px;
        height: 38px;
        line-height: 38px;
        margin: 21px 0;
        color: #fff;
        background: #55b6da;
        border-radius: 30px;
        text-align: center;
        cursor: pointer;
        user-select: none;
        &:hover {
          filter: brightness(80%);
        }
        &:active {
          filter: brightness(60%);
        }
      }
      .name {
        margin-right: 55px;
        font-weight: bold;
      }
    }
  }
  .mian-body {
    .main-side {
      display: inline-block;
      height: calc(100% - 140px);
      position: fixed;
      top: 120px;
      width: 240px;
      left: 90px;
      background: #fff;
      box-shadow: 0 1px 4px 0 rgba(58, 62, 81, 0.1);
      .title {
        position: relative;
        text-align: left;
        margin: 23px 0px 0px 12px;
        .title_border {
          display: inline-block;
          width: 4px;
          height: 26px;
          background: #33394b;
          border-radius: 15px;
          position: absolute;
          top: 0;
          bottom: 0;
          margin: auto;
        }
        .title_content {
          margin-left: 14px;
          font-size: 18px;
          font-weight: 600;
          color: #27274a;
        }
      }
      .card-content-list {
        height: calc(100% - 38px);
        overflow: auto;
        .card-content {
          padding: 0 12px 0 12px;
          position: relative;
          .card-content-title {
            font-size: 14px;
            color: #27274a;
            font-weight: 600;
            padding-bottom: 12px;
            padding-top: 20px;
          }
          .box-list {
            padding-bottom: 0;
            position: relative;
            left: -5px;
            font-size: 0;
            margin-right: -15px;
            .box {
              height: auto;
              line-height: unset;
              position: relative;
              margin-bottom: 15px;
              font-size: 14px;
              width: 35px;
              margin-top: unset;
              margin-right: unset;
              display: inline-block;
              .checki {
                border: 1px solid #dcdfe6;
                color: #dcdfe6;
                width: 27px;
                height: 27px;
                text-align: center;
                display: inline-block;
                line-height: 27px;
                background: #fff;
                border-radius: 50%;
                cursor: pointer;
              }
              .checked {
                color: #fff;
                background: #55b6da;
              }
              .check-error {
                color: #fff;
                background: #ec6941;
              }
            }
          }
        }
      }
    }
    .main-center {
      margin: 120px 200px 0px 200px;
      height: 100vh;
      .body-wrapper {
        color: #27274a;
        width: 100%;
        background: #ffffff;
        border: 1px solid #dedede;
        border-radius: 4px;
        .questions-title {
          font-size: 18px;
          line-height: 25px;
          padding: 18px 20px;
          background: #fafafa;
          border-bottom: 1px solid #dedede;
        }
        .questions-content {
          padding-left: 30px;
          padding-right: 75px;
        }
        .question-content {
          border-bottom: 1px solid #dedede;
          padding: 30px 0;
          position: relative;
          &:last-child {
            border: none;
          }
          .exam-question {
            font-size: 16px;
            line-height: 22px;
            margin-bottom: 10px;
            padding-left: 20px;
            position: relative;
            .question-index {
              color: #55b6da;
              position: absolute;
              left: -25px;
              top: 0;
              display: inline-block;
              width: 40px;
              line-height: 22px;
              text-align: right;
            }
          }
          .analysis {
            overflow: auto;
            background: rgba(222, 222, 222, 0.2);
            border-radius: 4px;
            padding: 15px 20px;
            line-height: 24px;
            margin-top: 10px;
            position: relative;
            .question-icon-wrapper {
              position: absolute;
              right: 10px;
              top: 14px;
              .sign {
                width: 48px;
                height: 28px;
                position: absolute;
                color: #fff;
                top: 10px;
                right: 0;
                font-size: 14px;
                border-radius: 2px;
                line-height: 28px;
                text-align: center;
                font-style: normal;
              }
              .icon-error {
                background: #ec6941;
              }
              .icon-right {
                background: #55b6da;
              }
            }
            .analysis-row {
              font-size: 14px;
              margin-top: 10px;
              min-height: 24px;
              padding-left: 80px;
              padding-right: 60px;
              position: relative;
              .analysis-title {
                position: absolute;
                width: 80px;
                left: 0;
                top: 0;
              }
              .question-analysis {
                text-align: justify;
              }
              .error {
                color: #f72a3a;
                font-weight: bold;
              }
            }
          }
        }
      }
    }
    .main-right {
      right: 90px;
      bottom: 20px;
      position: fixed;
      top: 120px;
      width: 120px;
      .nav {
        color: #27274a;
        line-height: 20px;
        padding: 0 10px;
        background: #ffffff;
        box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.1);
        border-radius: 4px;
        margin-bottom: 10px;
        text-align: center;
        .menu-item {
          padding: 14px 0;
          border-bottom: 1px solid #dedede;
        }
        .menu-item:last-child {
          border: none;
        }
        .pass {
          color: #55b6da;
        }
        .item-result,
        .unpass {
          color: rgb(236, 105, 65);
        }
        .item-label {
          margin-bottom: 6px;
        }
        .item-data {
          font-size: 18px;
          line-height: 22px;
          color: #ff0000;
          font-weight: 400;
        }
        .item-press {
          line-height: 17px;
          margin-bottom: 7px;
          color: #27274a;
          font-size: 14px;
          & span:nth-child(1)::after {
            content: "/";
            margin: 0 5px;
          }
        }
      }
      .btn {
        bottom: -10px;
        position: absolute;
        width: 100%;
        margin-bottom: 10px;
        cursor: pointer;
        text-align: center;
        height: 38px;
        line-height: 38px;
        border-radius: 20px;
        color: #fff;
        background: #33394b;
        user-select: none;
        &:hover {
          filter: brightness(120%);
        }
        &:active {
          filter: brightness(60%);
        }
      }
    }
  }
}
</style>